package org.example;

import java.util.logging.Level;
import java.util.logging.Logger;

public class MyUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {

    private String name;

    public MyUncaughtExceptionHandler(String name) {
        this.name = name;
    }

    @Override
    public void uncaughtException(Thread t, Throwable e) {
        Logger logger = Logger.getAnonymousLogger();
        logger.log(Level.WARNING, "线程异常,终止啦-" + t.getName() + " 异常是 " + e.getMessage() + e.toString() +"---"+name );
        System.out.println(name + "捕获了异常" + t.getName() + "异常");
        StackTraceElement[] stackTrace = e.getStackTrace();
        for (int i = 0; i < stackTrace.length; i++) {
            System.out.println(stackTrace[i].getClassName() + " --- " +
                    stackTrace[i].getFileName() + " --- " + stackTrace[i].getMethodName() + " --- " + stackTrace[i].getLineNumber()
            );
        }
        System.out.println("----e is ---" + e);
    }
}
